//商品分类全局组件的仓库

import { defineStore } from 'pinia'
import { reqCategory1, reqCategory2, reqCategory3 } from '@/api/product/attr/index'
import type { ICategoryResponseData, ICategoryObj } from '@/api/product/attr/type'
import type { ICategoryState } from '@/store/modules/types/type'

const useCategoryStore = defineStore('Category', {
  state: (): ICategoryState => {
    return {
      //存储一级分类的数组
      c1Attrs: [],
      //存储一级分类的Id
      c1Id: '',
      //存储二级分类的数组
      c2Attrs: [],
      //存储二级分类的Id
      c2Id: '',
      //存储三级分类的数组
      c3Attrs: [],
      //存储三级分类的Id
      c3Id: '',
    }
  },
  actions: {
    //获取一级分类的方法
    async getC1() {
      //发送请求获取一级分类数据
      let result: ICategoryResponseData = await reqCategory1()
      if (result.code === 200) {
        this.c1Attrs = result.data
        console.log(this.c1Attrs)
      }
    },
    //获取二级分类的方法
    async getC2() {
      //发送请求获取一级分类数据
      let result: ICategoryResponseData = await reqCategory2(this.c1Id)
      if (result.code === 200) {
        this.c2Attrs = result.data
        console.log(this.c2Attrs)
      }
    },
    //获取三级分类的方法
    async getC3() {
      //发送请求获取一级分类数据
      let result: ICategoryResponseData = await reqCategory3(this.c2Id)
      if (result.code === 200) {
        this.c3Attrs = result.data
        console.log(this.c3Attrs)
      }
    },
  },
  getters: {},
})

export default useCategoryStore
